package com.jrx.anytxn.transaction.mapper.ext;

import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 还款流水扩展
 *
 * @author haopu.zheng
 * date: 2019/8/24
 **/
public interface ExtTlPaymentLogMapper {

    /**
     * 根据customerId、loanId查询还款流水tl_payment_log，统计存在sum_status非最终态（S/F)的记录
     *
     * @param customerId 客户id
     * @param status     状态数组
     * @param tenantId   租户id
     * @return int
     * @author : zhenghaopu
     * date: 2019/9/25
     */
    List<TlPaymentLog> selectByCustomerIdAndSumStatus(@Param(value = "customerId") String customerId,
                                                      @Param(value = "status") String[] status,
                                                      @Param(value = "tenantId") String tenantId);

    /**
     * 根据customerId、loanId查询还款流水tl_payment_log，统计存在sum_status非最终态（S/F)的记录
     *
     * @param customerId 客户id
     * @param status     状态数组
     * @param tenantId   租户id
     * @param products   产品id
     * @return int
     * @author : zhenghaopu
     * date: 2019/9/25
     */
    List<TlPaymentLog> selectByCustomerIdProductsAndSumStatus(@Param(value = "customerId") String customerId,
                                                              @Param(value = "status") String[] status,
                                                              @Param(value = "tenantId") String tenantId,
                                                              @Param("productIds") List<String> products);

    /**
     * 根据还款流水号查询流水状态
     *
     * @param paymentSeqNo 还款流水
     * @param customerId   客户id
     * @param tenantId     租户id
     * @return 还款流水信息
     */
    TlPaymentLog selectPaymentStatusByPaymentSeqNo(@Param(value = "paymentSeqNo") String paymentSeqNo,
                                                   @Param(value = "customerId") String customerId,
                                                   @Param(value = "tenantId") String tenantId);

    /**
     * 根据贷款ID/还款流水状态/客户号查询还款流水
     *
     * @param loanId     贷款ID
     * @param status     还款流水状态
     * @param customerId 客户号
     * @param tenantId   租户id
     * @return com.jrx.anytxn.transaction.entity.TlPaymentLog 还款流水信息
     * @author zhenghaopu
     * date: 2019/8/19 15:50
     */
    TlPaymentLog selectPaymentLogByLoanIdAndStatus(@Param(value = "loanId") String loanId,
                                                   @Param(value = "status") String status,
                                                   @Param(value = "customerId") String customerId,
                                                   @Param(value = "paymentType") String paymentType,
                                                   @Param(value = "tenantId") String tenantId);

    /**
     * 批量插入还款流水
     *
     * @param tlPaymentLogs 还款流水信息
     */
    int batchInsert(List<TlPaymentLog> tlPaymentLogs);

    /**
     * 根据主键、客户id及租户id修改还款流水
     *
     * @param tlPaymentLog 还款流水
     */
    int updateByIdAndCustomerIdAndTenantId(TlPaymentLog tlPaymentLog);

    /**
     * 根据请求流水号查询流水状态
     *
     * @param payId      请求流水号
     * @param customerId 客户id
     * @param tenantId   租户id
     * @return 还款流水信息
     */
    TlPaymentLog selectByPayId(@Param(value = "payId") String payId,
                               @Param(value = "customerId") String customerId,
                               @Param(value = "tenantId") String tenantId);


    /**
     * 信用就医替换 - selectByPayId
     *
     * @param payId
     * @param customerId
     * @param tenantId
     * @return
     */
    TlPaymentLog selectByProductsAndPayId(@Param(value = "payId") String payId,
                                          @Param(value = "customerId") String customerId,
                                          @Param(value = "tenantId") String tenantId,
                                          @Param("productIds") List<String> products
    );

    /**
     * 批量 - 根据状态查询还款流水
     *
     * @param sumStatus
     * @return
     */
    List<TlPaymentLog> selectBySumStatusNotPayType(@Param(value = "sumStatus") String sumStatus,
                                                   @Param(value = "payTypeList") List<String> payTypeList,
                                                   @Param(value = "grayVersion") String grayVersion);

    /**
     * 批量更新还款流水（用于联机）
     *
     * @param updateLogList
     * @return
     */
    int batchUpdateLog(List<TlPaymentLog> updateLogList);

    /**
     * 批量 - 根据状态查询还款流水
     *
     * @param sumStatus
     * @return
     */
    List<TlPaymentLog> selectBySumStatus(@Param(value = "sumStatus") String sumStatus,
                                         @Param(value = "grayVersion") String grayVersion);

    /**
     * 批量 - 根据状态查询还款流水
     *
     * @param sumStatus
     * @return
     */
    List<TlPaymentLog> selectBySumStatusForAllocation(@Param(value = "sumStatus") String sumStatus, @Param(value = "grayVersion") String grayVersion);


    /**
     * 批量 - 根据状态、还款类型、业务日期查询还款流水
     * 批扣初始化使用
     *
     * @param sumStatus    最终态
     * @param paymentType  还款类型
     * @param businessDate 业务日期
     * @return 还款流水
     */
    List<TlPaymentLog> selectBySumStatusAndPayTypeForBatch(@Param(value = "sumStatus") String sumStatus,
                                                           @Param(value = "paymentType") String paymentType,
                                                           @Param(value = "businessDate") Date businessDate);

    /**
     * 批扣 - 根据状态、还款类型、业务日期查询还款流水
     * 批量扣款
     *
     * @param sumStatus    最终态
     * @param paymentType  还款类型
     * @param businessDate 业务日期
     * @return 还款流水
     */
    List<TlPaymentLog> selectBySumStatusAndBusinessDateForBatch(@Param(value = "sumStatus") String sumStatus,
                                                                @Param(value = "paymentType") String paymentType,
                                                                @Param(value = "businessDate") Date businessDate);

    /**
     * 根据最终态、还款类型、渠道、客户查询还款流水
     *
     * @param customerId    客户
     * @param channel       渠道
     * @param paymentType   还款类型
     * @param sumStatusList 状态列表
     * @return 还款流水
     */
    List<TlPaymentLog> selectListBySumStatusAndPayTypeForBatch(@Param("customerId") String customerId,
                                                               @Param("channel") String channel,
                                                               @Param("paymentType") String paymentType,
                                                               @Param("sumStatusList") List<String> sumStatusList);

    /**
     * 批量-批量更新还款流水（用于批量）
     *
     * @param updateLogList
     * @return
     */
    int batchUpdateLogForBatch(List<TlPaymentLog> updateLogList);


    /**
     * 根据还款流水号 查询 流水表
     *
     * @param seqNo 还款交易流水号
     * @return 还款流水信息
     */
    TlPaymentLog selectBySeqNoAndTenantId(@Param(value = "seqNo") String seqNo, @Param(value = "tenantId") String tenantId);

    /**
     * 按流水号及状态查询还款流水列表
     *
     * @param seqNoList  流水号列表
     * @param statusList 状态列表
     * @param customerId 客户id
     * @param tenantId   租户id
     * @return 还款流水列表
     */
    List<TlPaymentLog> selectBySeqNoListAndStatusList(@Param(value = "seqNoList") List<String> seqNoList,
                                                      @Param(value = "statusList") List<String> statusList,
                                                      @Param(value = "productIds") List<String> productIds,
                                                      @Param(value = "customerId") String customerId,
                                                      @Param(value = "tenantId") String tenantId);

    /**
     * 根据最终态、渠道、客户查询还款流水
     *
     * @param customerId    客户
     * @param channel       渠道
     * @param sumStatusList 状态列表
     * @return 还款流水
     */
    List<TlPaymentLog> selectListBySumStatusForBatch(@Param("customerId") String customerId,
                                                     @Param("channel") String channel,
                                                     @Param("sumStatusList") List<String> sumStatusList);

    /**
     * 查询代偿回购流水
     * 批量扣款
     *
     * @param sumStatus       最终态
     * @param paymentTypeList 还款类型
     * @param businessDate    业务日期
     * @return 还款流水
     */
    List<TlPaymentLog> selectBySumStatusAndPaymentTypForBatch(@Param(value = "sumStatus") String sumStatus,
                                                              @Param(value = "paymentTypeList") List<String> paymentTypeList,
                                                              @Param(value = "businessDate") Date businessDate);

    /**
     * 批量 - 根据状态查询还款流水
     *
     * @param sumStatus
     * @return
     */
    List<TlPaymentLog> selectBySumStatusAndPaymentTypeList(@Param(value = "sumStatus") String sumStatus,
                                                           @Param(value = "payTypeList") List<String> payTypeList);


    List<TlPaymentLog> selectByCollateDate(@Param("collateDate") Date collateDate,
                                           @Param("sumStatus") String sumStatus);


    int countByProductIdAndSumStatusAndBusinessDate(@Param("productId") String productId,
                                                    @Param("tenantId") String tenantId,
                                                    @Param("sumStatus") String sumStatus,
                                                    @Param("businessDate") Date businessDate);


    List<TlPaymentLog> selectByProductIdAndBusiDate(@Param("productId") String productId,
                                                    @Param("tenantId") String tenantId,
                                                    @Param("sumStatus") String sumStatus,
                                                    @Param("businessDate") Date businessDate);

    List<TlPaymentLog> selectByProIdAndBusiDateAndType(@Param("productId") String productId,
                                                       @Param("tenantId") String tenantId,
                                                       @Param("sumStatus") String sumStatus,
                                                       @Param("businessDate") Date businessDate,
                                                       @Param("paymentType") String paymentType);

    List<TlPaymentLog> selectByProIdAndAntoPaymentType(@Param("productId") String productId,
                                                       @Param("tenantId") String tenantId,
                                                       @Param("sumStatus") String sumStatus,
                                                       @Param("businessDate") Date businessDate,
                                                       @Param("paymentType") String paymentType,
                                                       @Param("autoPaymentType") String autoPaymentType);

    /**
     * 查询是否存在未完成的批扣流水
     */
    int countByAutoPaymentTypeAndSumStatusList(@Param("sumStatusList") List<String> sumStatusList,
                                               @Param("businessDate") Date businessDate,
                                               @Param("paymentType") String paymentType,
                                               @Param("autoPaymentType") String autoPaymentType);

    List<TlPaymentLog> selectByCondSelectivePage(@Param("tenantId") String tenantId, @Param("channel") String channel,
                                                 @Param("customerId") String customerId, @Param("seqNo") String seqNo,
                                                 @Param("beginDate") String beginDate, @Param("endDate") String endDate);

    TlPaymentLog selectPaymnetBySeqNoAndCusId(@Param("tenantId") String tenantId, @Param("customerId") String customerId, @Param("seqNo") String seqNo);

    int countBySumStatusAndBusinessDate(@Param("tenantId") String tenantId,
                                        @Param("sumStatus") String sumStatus,
                                        @Param("businessDate") Date businessDate);

    int countBySumStatusAndBusinessDateAndThirdChannel(
            @Param("tenantId") String tenantId,
            @Param("sumStatus") String sumStatus,
            @Param("businessDate") Date businessDate,
            @Param("thirdChannel") String thirdChannel,
            @Param("paymentType") String paymentType);

    BigDecimal sumBySumStatusAndBusinessDate(@Param("tenantId") String tenantId,
                                             @Param("sumStatus") String sumStatus,
                                             @Param("businessDate") Date businessDate);

    BigDecimal sumByPayId(@Param("tenantId") String tenantId,
                          @Param("payId") String payId);

    BigDecimal sumBySumStatusAndBusinessDateAndThirdChannel(
            @Param("tenantId") String tenantId,
            @Param("sumStatus") String sumStatus,
            @Param("businessDate") Date businessDate,
            @Param("thirdChannel") String thirdChannel,
            @Param("paymentType") String paymentType);

    List<TlPaymentLog> selectByTenantIdAndBusDate(@Param("tenantId") String tenantId,
                                                  @Param("businessDate") Date businessDate);

    List<TlPaymentLog> selectByTenantIdAndBusDateAndPaymentType(@Param("tenantId") String tenantId,
                                                                @Param("businessDate") Date businessDate,
                                                                @Param("paymentType") String paymentType,
                                                                @Param("thirdChannel") String thirdChannel);

    List<TlPaymentLog> selectByTenantAndDateAndChannel(@Param("tenantId") String tenantId, @Param("businessDate") Date businessDate, @Param("thirdChannel") String thirdChannel);

    /**
     * 批量插入还款流水
     *
     * @param tlPaymentLogs 还款流水信息
     */
    int batchInsertByMove(List<TlPaymentLog> tlPaymentLogs);


    /**
     * 查询非足额，扣款成功的流水
     * 批量扣款
     *
     * @param sumStatus    最终态
     * @param paymentType  还款类型
     * @param businessDate 业务日期
     * @return 还款流水
     */
    List<TlPaymentLog> selectBySumStatusAndSettleFlag(@Param(value = "sumStatus") String sumStatus,
                                                      @Param(value = "paymentType") String paymentType,
                                                      @Param(value = "businessDate") Date businessDate,
                                                      @Param(value = "settleFlag") String settleFlag);

    /**
     * 查询代偿回购流水
     *
     * @param sumStatus       最终态
     * @param paymentTypeList 还款类型
     * @param businessDate    业务日期
     * @return 还款流水
     */
    List<TlPaymentLog> selectByDistinctForBatch(@Param(value = "sumStatus") String sumStatus,
                                                @Param(value = "paymentTypeList") List<String> paymentTypeList,
                                                @Param(value = "businessDate") Date businessDate);


    /**
     * 根据条件查询 流水表
     */
    TlPaymentLog selectBySeqNoAndTenantIdAndCust(@Param(value = "seqNo") String seqNo,
                                                 @Param(value = "tenantId") String tenantId,
                                                 @Param(value = "customerId") String customerId);

    /**
     * 根据客户编号，对账日期查询还款流水
     *
     * @param customerId
     * @param tenantId
     * @param collateDate
     * @param sumStatus
     * @return
     */
    List<TlPaymentLog> selectByCollateDateAndCustomerId(@Param(value = "customerId") String customerId,
                                                        @Param(value = "tenantId") String tenantId,
                                                        @Param("collateDate") Date collateDate,
                                                        @Param("sumStatus") String sumStatus);

    /**
     * 根据客户编号，对账日期查询还款流水
     *
     * @param customerId
     * @param tenantId
     * @param collateDate
     * @param sumStatus
     * @return
     */
    List<TlPaymentLog> selectByCollateDateAndCustomerIdProducts(@Param(value = "customerId") String customerId,
                                                        @Param(value = "tenantId") String tenantId,
                                                        @Param("collateDate") Date collateDate,
                                                        @Param("sumStatus") String sumStatus,
                                                        @Param("productIds") List<String> productIds);

    TlPaymentLog selectByPayIdAndPaymentType(@Param("payId") String payId,
                                             @Param("paymentType") String paymentType,
                                             @Param("thirdChannel") String thirdChannel,
                                             @Param("tenantId") String tenantId);


    List<TlPaymentLog> selectByCustAndTenantIdAndNotSumSatusDesc(@Param(value = "customerId") String customerId,
                                                                 @Param(value = "tenantId") String tenantId,
                                                                 @Param(value = "sumStatus") String sumStatus);

    /**
     * 信用就医替换 selectByCustAndTenantIdAndNotSumSatusDesc
     * @param customerId
     * @param tenantId
     * @param sumStatus
     * @param products
     * @return
     */
    List<TlPaymentLog> selectByCustTenantIdProductIdsAndNotSumStatusDesc(@Param(value = "customerId") String customerId,
                                                                         @Param(value = "tenantId") String tenantId,
                                                                         @Param(value = "sumStatus") String sumStatus, @Param("productIds") List<String> products);

    /**
     * 微信还款对账场景中，考虑到切日情况，查询指定日期及其后一天以最大可能范围查询记录
     * @param tenantId
     * @param businessDate
     * @param thirdChannel
     * @return
     */
    List<TlPaymentLog> selectForBillComparing(@Param("tenantId") String tenantId, @Param("businessDate") Date businessDate, @Param("thirdChannel") String thirdChannel);


    /**
     *  获取上次批扣失败客户本次批扣金额
     * @param customerId 客户号
     * @param productIds 产品Id列表
     * @param sumStatus  扣款状态
     * @param paymentType 还款类型
     * @param businessDate 业务日期
     * @param autoPaymentType 批扣类型 第几次批扣
     * @return
     */
    int countCustProudDistinctForBatch(@Param(value = "customerId") String customerId,
                                       @Param("productIds") List<String> productIds,
                                       @Param(value = "sumStatus") String sumStatus,
                                       @Param(value = "paymentType") String paymentType,
                                       @Param(value = "businessDate") Date businessDate,
                                       @Param(value = "autoPaymentType") String autoPaymentType);
}